<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Cate extends Model
{

    //表
    protected $table = 'category';

    //主键
    protected $primaryKey = 'cate_id';

    //时间字段
    public $timestamps = false;

    //允许操作的字段
//    protected $fillable=[];
    protected $guarded = [];


    //格式化分类数据
    public function tree(){
        //获取所有分类数据
        $cates = $this->orderBy('cate_order','asc')->get();

        //格式化（排序，二级类缩进）
        return $this->getTree($cates);
    }

    public function getTree($category){
        //存放最终排完序的分类数据
        $arr = [];
        //排序，先获取一级类
        foreach ($category as $k=>$v){
            //一级类
            if ($v->cate_pid == 0){
                $arr[] = $v;
                //获取一级类下的二级类
                foreach ($category as $m=>$n){
                    if($v->cate_id==$n->cate_pid){
                        //给二级分类名称添加缩进
                        $n->cate_name='|----'.$n->cate_name;
                        $arr[] = $n;
                    }
                }
            }
        }
        return $arr;
    }
    //定义跟文章表的关联属性
    public function article(){
        return $this->hasMany('App\Model\Article','cate_id','cate_id');
    }
}
